package class01;

import java.util.Arrays;

/**
 * @author zhangchaoliang
 * create 2022
 */
public class e4 {
    public static int nearIndex(int[] arr,int value){
        int L=0;
        int R=arr.length-1;
        int index=-1;
        while (L<=R){
            int mid = L+((R-L)>>1);
            if (arr[mid]>=value){
                index=mid;
                R=mid-1;
            }else {
                L=mid+1;
            }
        }
        return index;
    }

    public static int test(int[] arr,int value){
        for (int i=0;i<arr.length;i++){
            if (arr[i]>=value)
                return i;
        }
        return -1;
    }


    public static int[] RandomArrays(int maxSize,int maxValue){
        int[] arr = new int[(int)((maxSize+1)*Math.random())];
        for (int i =0;i<arr.length;i++){
            arr[i]=(int)((maxValue+1)*Math.random())-(int)((maxValue+1)*Math.random());
        }
        return arr;
    }


    public static void main(String[] args) {
        int testTime = 500000;
        int maxSize=100;
        int maxValue=100;
        boolean succed = true;
        for (int i=0;i<testTime;i++){
            int[] arr = RandomArrays(maxSize,maxValue);
            Arrays.sort(arr);
            int value = (int)((maxValue+1)*Math.random())-(int)((maxValue+1)*Math.random());

            if(nearIndex(arr,value)!=test(arr,value)){
                succed=false;
                break;
            }
        }
        System.out.println(succed?"成功":"失败");
    }
}
